/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ACSetup.java
 *
 * Created on Jul 4, 2012, 10:35:18 AM
 */

package com.apps.ui;

import com.apps.beans.AccountBean;
import com.apps.beans.EnvironmentBean;
import com.apps.controllers.AccountSetupManager;
import com.apps.utils.StartupHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author ARIJIT PAL
 */
public class AccountSetupScreen extends javax.swing.JFrame {

    private ArrayList<EnvironmentBean> envArrList;
    private ArrayList<AccountBean> accArrList;
    private int accPos;
    private Map<String, Boolean> touchMap;
    /** Creates new form ACSetup */
    public AccountSetupScreen() throws Exception {
        initComponents();
        init();
    }

    public Map<String, Boolean> getTouchMap(){
        return touchMap;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        hostText = new javax.swing.JTextField();
        portText = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        userNameText = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        passwordText = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        pathText = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        scriptPathText = new javax.swing.JTextField();
        scriptPathBrowserAcc = new javax.swing.JButton();
        indicatorAcc = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        envList = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        pathEnvText = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        scriptPathEnvText = new javax.swing.JTextField();
        scriptPathBrowserEnv = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        preAcc = new javax.swing.JButton();
        nextAcc = new javax.swing.JButton();
        saveAcc = new javax.swing.JButton();
        delAcc = new javax.swing.JButton();
        newAcc = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        saveEnv = new javax.swing.JButton();
        delEnv = new javax.swing.JButton();
        newEnv = new javax.swing.JButton();

        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel2.setText("Host:");

        jLabel3.setText("Port:");

        jLabel4.setText("Username:");

        jLabel5.setText("Password:");

        jLabel6.setText("Remote Script:");

        jLabel7.setText("Script Path:");

        scriptPathBrowserAcc.setText("...");

        indicatorAcc.setText("0/0");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(indicatorAcc, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(hostText)
                            .addComponent(passwordText)
                            .addComponent(portText, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(userNameText, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(pathText, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(scriptPathText)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(scriptPathBrowserAcc, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(indicatorAcc)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(hostText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(portText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(userNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(passwordText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(pathText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(scriptPathText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(scriptPathBrowserAcc)))
        );

        envList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select Environment" }));
        envList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setEnvInfo(evt);
            }
        });

        jLabel1.setText("Environment Name: ");

        jLabel9.setText("Remote Script:");

        jLabel10.setText("Script Path:");

        scriptPathBrowserEnv.setText("...");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel9))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pathEnvText)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(scriptPathEnvText)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(scriptPathBrowserEnv, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(envList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(envList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(pathEnvText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(scriptPathEnvText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(scriptPathBrowserEnv))
                .addContainerGap())
        );

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel8.setText("Account Settings:");

        preAcc.setText("<< Previous");
        preAcc.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                accPreClicked(evt);
            }
        });

        nextAcc.setText("Next >>");
        nextAcc.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                accNextClicked(evt);
            }
        });

        saveAcc.setText("Save");
        saveAcc.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                accSaveClicked(evt);
            }
        });

        delAcc.setText("Delete");
        delAcc.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                accDelClicked(evt);
            }
        });

        newAcc.setText("New");
        newAcc.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                accNewClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(preAcc)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nextAcc, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveAcc, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(delAcc, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(newAcc, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(preAcc)
                    .addComponent(nextAcc)
                    .addComponent(saveAcc)
                    .addComponent(delAcc)
                    .addComponent(newAcc))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel11.setText("Environment Settings:");

        saveEnv.setText("Save");
        saveEnv.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                envSaveClicked(evt);
            }
        });

        delEnv.setText("Delete");
        delEnv.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                envDelClicked(evt);
            }
        });

        newEnv.setText("New");
        newEnv.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                envNewClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addGap(200, 200, 200)
                .addComponent(saveEnv, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(delEnv, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(newEnv, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(saveEnv)
                    .addComponent(delEnv)
                    .addComponent(newEnv))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void setEnvInfo(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setEnvInfo
        // TODO add your handling code here:
        try {
            EnvironmentBean env = (EnvironmentBean) envList.getSelectedItem();
            if (env != null) {
                pathEnvText.setText(env.getPath());
                scriptPathEnvText.setText(env.getScriptPath());
                initAcc();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_setEnvInfo

    private void envSaveClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_envSaveClicked
        // TODO add your handling code here:
        try {
            EnvironmentBean env = (EnvironmentBean) envList.getSelectedItem();
            env.setPath(pathEnvText.getText());
            env.setScriptPath(scriptPathEnvText.getText());
            try {
                AccountSetupManager.updateEnv(env);
                JOptionPane.showMessageDialog(this, "Record has been saved successfully");
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(this, "Error in saving the record");
            }
            touchMap.put(env.getCode(), true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_envSaveClicked

    private void envNewClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_envNewClicked
        // TODO add your handling code here:
        try {
            String newEnvName = JOptionPane.showInputDialog(this, "New Environment Name: ");
            if (newEnvName != null) {
                try {
                    AccountSetupManager.createEnv(newEnvName);
                    JOptionPane.showMessageDialog(this, "New Record has been saved successfully");
                } catch (Exception ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(this, "Error in saving the record");
                }
                init();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_envNewClicked

    private void envDelClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_envDelClicked
        // TODO add your handling code here:
        try {
            EnvironmentBean env = (EnvironmentBean) envList.getSelectedItem();
            int n = JOptionPane.showConfirmDialog(
                    this,
                    "You are about to delete '" + env.getName() + "'\nAre you sure?",
                    "Delete!!",
                    JOptionPane.YES_NO_OPTION);
            if (n == JOptionPane.YES_OPTION) {
                AccountSetupManager.deleteEnv(env);
            }
            init();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_envDelClicked

    private void accSaveClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_accSaveClicked
        // TODO add your handling code here:
        try{
            AccountBean bean = accArrList.get(accPos);
            bean.setHost(hostText.getText());
            bean.setPort(portText.getText());
            bean.setUserName(userNameText.getText());
            bean.setPassword(passwordText.getText());
            bean.setPath(pathText.getText());
            bean.setScriptPath(scriptPathText.getText());
            AccountSetupManager.updateAcc(bean);
            JOptionPane.showMessageDialog(this, "Record has been saved successfully");
            touchMap.put(bean.getEnvCode(), true);
        }catch(Exception ex){
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "Error in saving the record");
        }

    }//GEN-LAST:event_accSaveClicked

    private void accNewClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_accNewClicked
        // TODO add your handling code here:
        try {
            EnvironmentBean env = (EnvironmentBean) envList.getSelectedItem();
                try {
                    AccountSetupManager.createAcc(Integer.valueOf(env.getCode()));
                    JOptionPane.showMessageDialog(this, "New Record has been saved successfully");
                } catch (Exception ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(this, "Error in saving the record");
                }
                saveAcc.setEnabled(true);
                touchMap.put(env.getCode(), true);
                initAcc();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_accNewClicked

    private void accDelClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_accDelClicked
        // TODO add your handling code here:
        try {
            AccountBean acc = accArrList.get(accPos);
            int n = JOptionPane.showConfirmDialog(
                    this,
                    "You are about to delete '" + acc.getHost() + "'\nAre you sure?",
                    "Delete!!",
                    JOptionPane.YES_NO_OPTION);
            if (n == JOptionPane.YES_OPTION) {
                AccountSetupManager.deleteAcc(acc);
            }
            initAcc();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_accDelClicked

    private void accPreClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_accPreClicked
        // TODO add your handling code here:
        getPreviousAcc();
    }//GEN-LAST:event_accPreClicked

    private void accNextClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_accNextClicked
        // TODO add your handling code here:
        getNextAcc();
    }//GEN-LAST:event_accNextClicked

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_formWindowClosed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        System.out.println("end");
        String envCode = StartupHelper.getSettings().getApps_env_code();
        if(touchMap.get(envCode)!=null && touchMap.get(envCode)==true){
            StartupHelper.closeAllShell();
            //StartupHelper.doEnvStartup(envCode);
        }
    }//GEN-LAST:event_formWindowClosing

    //initial setup for the UI
    public void init() throws Exception {
        envArrList = AccountSetupManager.getEnvList();
        touchMap = new HashMap<String, Boolean>();
        envList.removeAllItems();
        for (EnvironmentBean env : envArrList) {
            envList.addItem(env);
            touchMap.put(env.getCode(), false);
        }
        initAcc();
    }
    public void initAcc(){
        nextAcc.setEnabled(false);
        preAcc.setEnabled(false);
        
        try{
            EnvironmentBean env = (EnvironmentBean) envList.getSelectedItem();
            accArrList = AccountSetupManager.getAccList(env.getCode());
            accPos = accArrList.size()>0?0:-1;
            indicatorAcc.setText((accPos+1)+" / "+accArrList.size());
            if(accArrList.size()>1){
                nextAcc.setEnabled(true);
            }
            if(accPos >= 0){
                setAccInfo(accArrList.get(accPos));
                saveAcc.setEnabled(true);
            }
            else
                saveAcc.setEnabled(false);
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    public void getNextAcc(){
        try{
            accPos++;
            if(accArrList.size()>accPos && accPos >=0){
                setAccInfo(accArrList.get(accPos));
                indicatorAcc.setText((accPos+1)+" / "+accArrList.size());
                if(accArrList.size()<=accPos+1){
                    nextAcc.setEnabled(false);
                    preAcc.setEnabled(true);
                    accPos = accArrList.size()-1;
                    return;
                }
                nextAcc.setEnabled(true);
                preAcc.setEnabled(true);
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    public void getPreviousAcc(){
        try{
            accPos--;
            if(accArrList.size()>accPos && accPos >=0){
                setAccInfo(accArrList.get(accPos));
                indicatorAcc.setText((accPos+1)+" / "+accArrList.size());
                if(accPos-1<0){
                    preAcc.setEnabled(false);
                    nextAcc.setEnabled(true);
                    accPos = 0;
                    return;
                }
                nextAcc.setEnabled(true);
                preAcc.setEnabled(true);
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    public void setAccInfo(AccountBean bean){
        if(bean != null){
            hostText.setText(bean.getHost());
            portText.setText(bean.getPort());
            userNameText.setText(bean.getUserName());
            passwordText.setText(bean.getPassword());
            pathText.setText(bean.getPath());
            scriptPathText.setText(bean.getScriptPath());
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[])throws Exception{
        System.out.println("Start");
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    new AccountSetupScreen().setVisible(true);
        
        /*java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try{
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    new AccountSetupScreen().setVisible(true);
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        });*/
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton delAcc;
    private javax.swing.JButton delEnv;
    private javax.swing.JComboBox envList;
    private javax.swing.JTextField hostText;
    private javax.swing.JLabel indicatorAcc;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JButton newAcc;
    private javax.swing.JButton newEnv;
    private javax.swing.JButton nextAcc;
    private javax.swing.JTextField passwordText;
    private javax.swing.JTextField pathEnvText;
    private javax.swing.JTextField pathText;
    private javax.swing.JTextField portText;
    private javax.swing.JButton preAcc;
    private javax.swing.JButton saveAcc;
    private javax.swing.JButton saveEnv;
    private javax.swing.JButton scriptPathBrowserAcc;
    private javax.swing.JButton scriptPathBrowserEnv;
    private javax.swing.JTextField scriptPathEnvText;
    private javax.swing.JTextField scriptPathText;
    private javax.swing.JTextField userNameText;
    // End of variables declaration//GEN-END:variables

}
